package net

import (
	"myPaxos/manager"
	"myPaxos/helper"
	"github.com/pkg/errors"
)

func ParseWrapper(ev *manager.Wrapper) (interface{}, error) {
	switch ev.Type {
	case manager.Vote_Wrapper:
		voteWrapper := &manager.VoteWrapper{}
		err := helper.DecodeWrapper(voteWrapper, ev.Payload)
		if err != nil {
			return nil, errors.New("parse vote wrapper error: " + err.Error())
		} else {
			return voteWrapper, nil
		}
	case manager.Msg_Wrapper:
		msgWrapper := &manager.MsgWrapper{}
		err := helper.DecodeWrapper(msgWrapper, ev.Payload)
		if err != nil {
			return nil, errors.New("parse msg wrapper error: " + err.Error())
		} else {
			return msgWrapper, nil
		}
	case manager.Ping_Wrapper:
		ping := &manager.Ping{}
		err := helper.DecodeWrapper(ping, ev.Payload)
		if err != nil {
			return nil, errors.New("parse Ping error: " + err.Error())
		} else {
			return ping, nil
		}
	default:
		return nil, errors.New("parse not support wrapper type")
	}
}
